package org.example.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.example.order.entity.User;
import org.example.order.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class UserService extends ServiceImpl<UserMapper, User> {
    private Logger logger = LoggerFactory.getLogger(UserService.class);
    private final UserMapper userMapper;

    public void initUser() {
        Random random = new Random();
        for(int i = 1; i < 2; i++) {
            List<User> users = new ArrayList<>();
            for(int j = 0; j < 5; j++) {
                User user = User.builder()
                        .userName("王老六")
                        .userAccount(UUID.randomUUID().toString().replace("-", "").substring(26))
                        .idCard("5122241997" + random.nextInt(100_000_000))
                        .phoneNumber("199" + random.nextInt(100_000_000))
                        .age(25)
                        .build();
                users.add(user);
            }
            logger.info("已初始化 {} 条数据", 500 * i + 500);
            Long start = System.currentTimeMillis();
            saveBatch(users);
            logger.info("耗时：{}", System.currentTimeMillis() - start);
        }
    }

    @SneakyThrows
    @Transactional
    public void addUser() {
        Random random = new Random();
        User user = User.builder()
                .userName("王老六")
                .userAccount(UUID.randomUUID().toString().replace("-", "").substring(26))
                .idCard("5122241997" + random.nextInt(100_000_000))
                .phoneNumber("199" + random.nextInt(100_000_000))
                .age(25)
                .build();
//        save(user);
        Thread.sleep(1000);
        long l = System.currentTimeMillis();
        Integer count = count();
        logger.info("已初始化 {} 条数据, 耗时：{}", count, System.currentTimeMillis() - l);

    }

}
